/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hibernate.dao;

import hibernate.entity.Centers;
import hibernate.entity.Courses;
import hibernate.entity.Students;
import java.util.List;
import org.hibernate.dialect.Dialect;

/**
 *
 * @author QuynhDu
 */
public class StudentDAO extends AbstractDAO {

    /**
     * @return List<Students>
     * @see Used to get all Student
     */
    public List<Students> readAll() {
        query = session.createQuery("from Students where status = 1");
        return query.list();
    }

    /**
     * @return List<Students>
     * @see Used to get all Student are living
     */
    public List<Students> readLiving() {
        query = session.createQuery("from Students where status > 0");
        return query.list();
    }

    /**
     * @param student
     * @return int
     * @see Used to create student
     */
    public int create(Students student) {
        try {
            tx = session.beginTransaction();
            session.save(student);
            tx.commit();
            return 1;
        } catch (Exception e) {
            return 0;
        }

    }

    public List<Students> search(String keyword) {
        query = session.createQuery("from Students st where st.accounts.fullName like '%" + keyword + "%' "
                + "or st.accounts.userName like '%" + keyword + "%' and st.status >0");
        return query.list();
    }

    /**
     * @param student
     * @return int
     * @see Used to update student
     */
    public int update(Students student) {
        try {
            int n = student.getStudentId();
            tx = session.beginTransaction();
            student = (Students) session.merge(student);
            session.update(student);
            tx.commit();
            return 1;
        } catch (Exception e) {
            return 0;
        }

    }

    /**
     * @param student
     * @return int
     * @see Used to remove student
     */
    public int remove(Students student) {
        try {
            tx = session.beginTransaction();
            student.setStatus(-1);
            student = (Students) session.merge(student);
            tx.commit();
            return 1;
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * @param id an id represent for some student to get his information
     * @return Students 
     */
    public Students readByID(int id) {
        try {
            query = session.createQuery("from Students where studentId = :id and status >0");
            query.setParameter("id", id);
            return (Students) query.list().get(0);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * @param i represent for some student's status
     * @return List<Students>
     */
    public List<Students> readByStatus(int i) {
        try {
            query = session.createQuery("from Students where status = :i");
            query.setParameter("i", i);
            return query.list();
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * @param Courses a course for filtering students who have not had their courses
     * @return List<Students>
     */
    public List<Students> outOfCourse(Courses c) {
        try {
            query = session.createQuery("select s from Students s WHERE :c not in elements (s.courseses) and s.status >0");
            query.setParameter("c", c);
            return query.list();
        } catch (Exception e) {
            return null;
        }
    }
}
